package project.meetemup.view;

import java.util.ArrayList;

import project.meetemup.R;
import project.meetemup.view.profile.MainProfileActivity;
import project.meetemup.view.profile.ProfileActivity;
import project.meetemup.view.profile.ProfileListActivity;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageView;

public class MainMenuActivity extends ActionBarActivity {
	// public Button listProfileButton;
	// public Button newProfileButton;
	// public Button chatButton;
	// public Button tendenciesButton;

	// public void createButton(Button buttonForCreate, int buttonId,
	// Class<Activity> classOfButton) {
	// buttonForCreate = (Button) findViewById(buttonId);
	// buttonForCreate.setOnClickListener(
	// new View.OnClickListener() {
	// public void onClick(View view) {
	// Intent myIntent = new Intent(view.getContext(),
	// classOfButton);
	// startActivityForResult(myIntent, 0);
	// }
	//
	// });
	// }
	private Intent myIntent = new Intent(this, MainProfileActivity.class);

	public OnItemClickListener bindEventsListner() {
		OnItemClickListener itemClickListener = null;	
		
		itemClickListener = new OnItemClickListener() {
			public void onItemClick(AdapterView parent, View v, int position, long id) {
				//Toast.makeText(MainMenuActivity.this, " position " + id, Toast.LENGTH_SHORT).show();
				//this.finish();
				//Intent myIntent = new Intent();
				//myIntent.setClass(this, MainMenuActivity.class);
//				myIntent.set
				//this, MainMenuActivity.class);
				// myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(myIntent);
		}
	};
		
		return itemClickListener;
	}
	
//	public OnItemSelectedListener myBindListner() {
//		OnItemSelectedListener item = null;
//		
//		item = new OnItemSelectedListener() {
//
//			@Override
//			public void onItemSelected(AdapterView<?> arg0, View arg1,
//					int arg2, long arg3) {
//				Intent intent = new Intent();
//				intent.setClassName(this, "DeviceListActivity");
//				startActivity(intent);
//			}
//
//			@Override
//			public void onNothingSelected(AdapterView<?> arg0) {
//				// TODO Auto-generated method stub
//				
//			}
//			
//		};
//		return item;
//	}
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		// buildMenu();

		Gallery gallery = (Gallery) findViewById(R.id.gallery);
		gallery.setAdapter(new ButtonAdapter(this));

		gallery.setOnItemClickListener(bindEventsListner());
		//gallery.setOnItemSelectedListener(myBindListner());
	}
	
	public void botaoTeste() {
		
	}

	public class ButtonAdapter extends BaseAdapter {
		int mGalleryItemBackground;
		private Context mContext;
		// private Button[] buttonsOfMenu;
		private ArrayList<Button> buttonsOfMenu = new ArrayList<Button>();
		private int[] buttons = {R.id.menuButton, R.id.menuButton, R.id.menuButton, R.id.menuButton
				, R.id.menuButton, R.id.menuButton, R.id.menuButton, R.id.menuButton, R.id.menuButton};
		private Integer[] mImageIds = { R.drawable.logo, R.drawable.logo,
				R.drawable.logo, R.drawable.logo, R.drawable.logo,
				R.drawable.logo, R.drawable.logo };

		public ButtonAdapter(Context c) {
			mContext = c;
			TypedArray attr = mContext.obtainStyledAttributes(R.styleable.MenuGallery);
			//buildButtonsOfMenu();
			// mGalleryItemBackground =
			// attr.getResourceId(R.styleable.HelloGallery_android_galleryItemBackground,
			// 0);
			attr.recycle();
		}

		public int getCount() {
			return buttons.length;
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView imageView = new ImageView(mContext);

			imageView.setImageResource(buttons[position]);
			imageView.setLayoutParams(new Gallery.LayoutParams(150, 100));
			imageView.setScaleType(ImageView.ScaleType.FIT_XY);
//			imageView.setBackgroundResource(mGalleryItemBackground);
			
//			LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//            View v = vi.inflate(R.layout.menu_button, null);
//            v.setMinimumWidth(getWindowManager().getDefaultDisplay().getWidth());
//            return v;

			
			return imageView;
		}
		
		public void buildButtonsOfMenu() {
			// TODO fix layout and remove this button (only have mockup
			// proposes)
			// Button listProfileButton =
			// (Button)findViewById(R.id.ListProfileStartButton);
			Button listProfileButton = new Button(getBaseContext());
			listProfileButton.setOnClickListener(new View.OnClickListener() {
				public void onClick(View view) {
					Intent myIntent = new Intent(view.getContext(),
							ProfileListActivity.class);
					startActivityForResult(myIntent, 0);
				}

			});
			listProfileButton.setText("List Profiles");
			buttonsOfMenu.add(listProfileButton);

			// Button newProfileButton = (Button)findViewById(R.id.NewProfileStartButton);
			Button newProfileButton = new Button(getBaseContext());
			newProfileButton.setOnClickListener(new View.OnClickListener() {
				public void onClick(View view) {
					Intent myIntent = new Intent(view.getContext(),
							ProfileActivity.class);
					startActivityForResult(myIntent, 0);
				}

			});
			newProfileButton.setText("New Profile");
			buttonsOfMenu.add(newProfileButton);

			// Button chatButton = (Button)findViewById(R.id.BTChatStartButton);
			Button chatButton = new Button(getBaseContext());
			chatButton.setOnClickListener(new View.OnClickListener() {
				public void onClick(View view) {
					Intent myIntent = new Intent(view.getContext(),
							BlueToothPresentationActivity.class);
					startActivityForResult(myIntent, 0);
				}

			});
			chatButton.setText("Chat");
			buttonsOfMenu.add(chatButton);

			// Button tendenciesButton = (Button)findViewById(R.id.TendenciesStartButton);
			Button tendenciesButton = new Button(getBaseContext());
			tendenciesButton.setOnClickListener(new View.OnClickListener() {
				public void onClick(View view) {
					Intent myIntent = new Intent(view.getContext(),
							ProfileActivity.class);
					startActivityForResult(myIntent, 0);
				}
			});
			tendenciesButton.setText("Tendencies");
			buttonsOfMenu.add(tendenciesButton);

//			arrMenuButtons = new ArrayAdapter<Button>(this,
//					R.layout.simple_gallery_item, buttons);
//			gallery.setAdapter(arrMenuButtons);
		}
	}
}
